<?php
class Category_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	
	public function get_all(){
		$query = $this->db->get('category');
		return $query->result();
	}

	public function get_updated($day){
		$this->db->where('category_modified_date >= DATE_SUB(NOW(), INTERVAL '.$day.' DAY)');
		$query = $this->db->get('category');
		return $query->result();
	}

	public function add($category_name){
		$data = array(
				'category_name' => utf8_encode($category_name)
		);
		if ($this->db->insert('category', $data))
			return $this->db->insert_id();
		else
			return -1;
	}

	public function update($category_id, $category_name){
		$data = array(
				'category_name' => utf8_encode($category_name)
		);
		$this->db->where('category_id', $category_id);
		if ($this->db->update('category', $data))
			return true;
		else
			return false;
	}

	public function get_id($category_name){
		//$query = $this->db->get_where('category', array('category_name' => $category_name));
		$query = $this->db->where('LOWER(category_name)', strtolower(utf8_encode($category_name)))->get('category');

		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row->category_id;
		}else
			return -1;
	}

	public function get_category_by_id($id){
		$query = $this->db->where('category_id', $id)->get('category');

		if ($query->num_rows() > 0)
		{
			return $query->row();
		}else
			return null;
	}

	public function search($string, $limit, $start){
		//if (strlen($string)>0){
			$this->db->like('category_name', $string, 'after');
			$this->db->or_like('category_name', ' '.$string);
			$this->db->limit($limit, $start);
			$query = $this->db->get('category');
			if ($query->num_rows() > 0)
			{
				return $query->result();
			}
		//}
		return null;
	}
	
	public function count_search($string){

		$this->db->like('category_name', $string, 'after');
		$this->db->or_like('category_name', ' '.$string);
		$this->db->from('category');
		return $this->db->count_all_results();
	}
}